package com.neusoft.EYSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.EYSystem.dao.LevelProjectLinkDAO;
import com.neusoft.EYSystem.dao.NursingLevelDAO;
import com.neusoft.EYSystem.dao.NursingProjectDAO;
import com.neusoft.EYSystem.po.*;
import com.neusoft.EYSystem.service.LevelProjectLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/levelProjectLink")
public class LevelProjectLinkController {

    // ✅ 工具方法：判断是否为合法字符串（排除 null、空串、"null"）
    private boolean isValid(String value) {
        return value != null && !value.trim().isEmpty() && !"null".equalsIgnoreCase(value.trim());
    }

    @Autowired
    private LevelProjectLinkService levelProjectLinkService;

    @Autowired
    private LevelProjectLinkDAO levelProjectLinkDAO;

    @Autowired
    private NursingLevelDAO nursingLevelDAO;

    @Autowired
    private NursingProjectDAO nursingProjectDAO;

    // 管理员端：护理级别 弹窗配置护理级别上表格
    // 管理员端： 护理管理 客户护理设置 客户表 弹窗配置客户护理项目中 点护理级别后显示下表格
    // 查询某护理级别已包含的护理项目,已包含护理项目中再查询
    // 支持name status查询
    @GetMapping("/getIncludedProjects")
    public PageResponseBean<List<NursingProject>> getIncludedProjects(
            Long cur, Long number,
            NursingLevel nursingLevel,
            NursingProject nursingProject) {
        return levelProjectLinkService.getIncludedProjects(cur, number, nursingLevel, nursingProject);
    }

    // 管理员端：护理级别 弹窗配置护理级别下表格
    // 查询某护理项目未包含护理项目，支持再查询
    // 支持根据name status查询
    @GetMapping("/getExcludedProjects")
    public PageResponseBean<List<NursingProject>> getExcludedProjects(Long cur, Long number,NursingLevel nursingLevel,NursingProject nursingProject) {
        return levelProjectLinkService.getExcludedProjects(cur, number, nursingLevel, nursingProject);
    }
    // 管理员端：护理级别 弹窗配置护理级别下表格添加按钮
    // 添加护理项目到护理级别
    @PostMapping("/addProjectToLevel")
    public ResponseBean<Integer> addProjectToLevel(NursingLevel nursingLevel, NursingProject nursingProject) {
        ResponseBean<Integer> response = new ResponseBean<>();
        LevelProjectLink levelProjectLink = new LevelProjectLink();
        levelProjectLink.setProjectId(nursingProject.getProjectId());
        levelProjectLink.setLevelId(nursingLevel.getLevelId());

        try {
            if(levelProjectLinkDAO.insert(levelProjectLink) > 0){
                response.setStatus(200);
                response.setMsg("护理级别添加护理项目成功");
                return response;
            }else {
                response.setStatus(400);
                response.setMsg("护理级别添加护理项目失败，可能是数据不完整或重复");
            }
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("护理级别添加护理项目失败: " + e.getMessage());
        }
        return response;
    }
    // 管理员端：护理级别 弹窗配置护理级别上表格移除按钮
    @PostMapping("/removeProjectFromLevel")
    public ResponseBean<Integer> removeProjectFromLevel(NursingLevel nursingLevel, NursingProject nursingProject) {
        ResponseBean<Integer> response = new ResponseBean<>();
        try {
            // 构造删除条件：根据 level_id 和 project_id 删除
            QueryWrapper<LevelProjectLink> wrapper = new QueryWrapper<>();
            wrapper.eq("level_id", nursingLevel.getLevelId())
                    .eq("project_id", nursingProject.getProjectId());

            int deleted = levelProjectLinkDAO.delete(wrapper);

            if (deleted > 0) {
                response.setStatus(200);
                response.setMsg("护理级别移除护理项目成功");
            } else {
                response.setStatus(400);
                response.setMsg("护理级别移除护理项目失败，记录不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(500);
            response.setMsg("护理级别移除护理项目失败: " + e.getMessage());
        }
        return response;
    }
}
